/***********定位地图***********/
var map = new AMap.Map('container1');
var Self_JD, Self_WD;
var num = 0; //最近商家的下标值
var min_JL = 20; //最小距离
var map, geolocation;
var objData; //json数据
//加载地图，调用浏览器定位服务
map = new AMap.Map('container1', {
	resizeEnable: true
});
map.plugin('AMap.Geolocation', function() {
	geolocation = new AMap.Geolocation({
		enableHighAccuracy: true, //是否使用高精度定位，默认:true
		timeout: 10000, //超过10秒后停止定位，默认：无穷大
		buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
		zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
//		buttonPosition: 'LB'
	});
	map.addControl(geolocation);
	geolocation.getCurrentPosition();
	AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息
});
//解析定位结果
function onComplete(data) {
	Self_JD = data.position.getLng();
	Self_WD = data.position.getLat();
	find(Self_JD, Self_WD)
}

/************************************************/
//找出最近的商家
function find(Self_JD, Self_WD) {
	htmlobj = $.ajax({
		url: "/Public/game/data.json",
		async: false,
		dataType: "json",
		success: function(obj) {
			objData = obj;
			for(k in obj) {
				var JL = Math.sqrt(Math.pow((Self_JD - obj[k].JD), 2) + Math.pow((Self_WD - obj[k].WD), 2));
				JL = JL.toFixed(5);
				console.log(JL);
				if(JL < min_JL) {
					min_JL = JL;
					num = k;
				}
			}
			console.log(obj[num]);
			map_shop(num);
		}
	});
}

/************************************************/
//商家的位置在地图上的显示
function map_shop(num) {
	var jd, wd, address;
	jd = objData[num].JD;
	wd = objData[num].WD;
	address = "最近兑换点:" +objData[num].shop_name +"<br/>地址:" + objData[num].address +  "<br/>联系人:"+objData[num].contact1+ "<br/>联系电话:" + objData[num].mobile;
	var lnglat = [jd, wd];
	var map = new AMap.Map('container', {
		resizeEnable: true,
		center: lnglat,
		zoom: 15
	});
	var marker = new AMap.Marker({
		position: lnglat
	});
	marker.setMap(map);

	var content = '<div class="info-title" >张裕白兰地线下兑换点</div><div class="info-content">' +
		'<img src="/Public/game/images/Homg_logo.png">' + address;
	var infowindow1 = new AMap.AdvancedInfoWindow({
		content: content,
		offset: new AMap.Pixel(0, -30)
	});
	infowindow1.open(map, lnglat);
}